草庐IT

TypeScript 条件语句

全部标签

javascript - 如何组合这些 jQuery 语句?

我有一个这样的jQuery语句;varcurrent=$(this);current.hide();current.siblings('.ab').hide();current.siblings('.cd').hide();我想把它改成一条语句,我写了;$(current,current.siblings('.ab'),current.siblings('.cd')).hide();但是ab并没有隐藏。如何将3个hide()语句合并为一个? 最佳答案 您可以使用multipleselector和addBack():$(this).s

javascript - '错误 : Uncaught (in promise): No provider for Jsonp' (HTML/Javascript/Typescript/Angular2)

错误截图:.ts文件代码(SearchDisplay.component.ts):import{Component,OnInit}from'angular2/core';import{Router}from'angular2/router';import{Hero}from'./hero';import{HeroService}from'./hero.service';import{RouteConfig,ROUTER_DIRECTIVES}from'angular2/router';import{HeroesComponent}from'./heroes.component';imp

javascript - typescript 要求模板文字

我正在使用typescript,并在连接字符串时提示,constcontent=senderDisplay+','+moment(timestamp).format('YY/MM/DD')+'at'+moment(timestamp).format('h:mmA');[tslint]Useatemplateliteralinsteadofconcatenatingwithastringliteral.(prefer-template)解决这个问题的模板文字是什么?干杯 最佳答案 你可以看到templateliteralsinMDN,

javascript - 为什么这个javascript 'If... Else...'语句中没有比较语句

在下面的代码中var$next=$active.next().length?$active.next():$('#slideshowIMG:first');'$active.next().length'部分似乎没有比较任何东西,我不明白条件是如何确定为True或False的。或者是说:如果各种$next等于$active.next().length则条件为真? 最佳答案 在javascript中,任何表达式都可以转换为真值或假值,因此在比较位置是有效的。在javascript中为false的值是错误0""(空字符串)null未定义N

javascript - js中的较短条件

我有这样的条件:if(foo=='fgfg'||foo=='asdf'||foo=='adsfasdf'){//dostuff}肯定有更快的写法吗?谢谢。 最佳答案 你可能会考虑一个switch-case语句switch(foo){case"fgfg":case"asdf":case"adsfasdf"://...}它并没有真正更短,但根据您使用的条件数量可能更具可读性。 关于javascript-js中的较短条件,我们在StackOverflow上找到一个类似的问题:

javascript - typescript 错误 : TS2304: Cannot find name '$'

声明x=$('#msgBox_'+scope.boxId).position().left;生成一个errorTS2304:Cannotfindname'$'尽管jquery和@types安装在node_modules文件夹中。我的tsconfig.json看起来像这样:{"compilerOptions":{"module":"commonjs","target":"es5","sourceMap":true,"moduleResolution":"node","declaration":true},"exclude":["node_modules"]}我该如何解决?

javascript - Jest with TypeScript : TypeError: environment. 拆解不是函数

当我尝试使用Jest运行测试时出现此错误:FAILsrc/__tests__/jokeGenerator.test.tsx●TestsuitefailedtorunTypeError:environment.teardownisnotafunctionatnode_modules/jest-runner/build/run_test.js:230:25我在这里遇到了一个可能的解决方案:HowtosolveTypeError:environment.teardownisnotafunction但在按照建议进行操作后:删除我的yarn.lock文件、node_modules文件夹、从我的p

javascript - 打破 .each() jquery 语句的最佳方法是什么?

我有以下代码:$.each(data.People,function(i,person){html.push("");});我想更改此代码,以便如果数组(data.People)的人数超过20人,它将对前20人执行上面的操作,然后只显示文字“X”多人。.例如,如果数组有100人,它将显示前20人,然后只说“还有80人......”我假设我需要在每个语句中使用一些计数器,但希望看到打破它并显示剩余文本的最佳方法。 最佳答案 returnfalse从jQuery中的each循环中中断,就像在普通JavaScript中for循环中的bre

javascript - 使用angular JS检查数组是否为空的条件语句

我有这个功能:$scope.doPaste=function(destination){if($scope.selectCopy.ids!=[]){console.log("willcopy");$scope.CopyFiles(destination);}if($scope.selectMove.ids!=[]){console.log("willmove");$scope.MoveFiles(destination);}};在我的应用中,$scope.selectMove.ids和$scope.selectCopy.ids不能都是非空的。我的意思是,例如当$scope.select

javascript - Javascript中带条件的数组排序

我需要按升序对数组进行排序,并将所有零放在末尾。例如[0,0,0,3,2,1]需要排序为[1,2,3,0,0,0]。这是我的代码,我需要添加什么以确保所有零都在末尾?functionsort_by_field(array,field){returnarray.sort(function(a,b){if(a[field]>b[field]){return1;}if(a[field]任何帮助将不胜感激。 最佳答案 你可以这样做:[0,0,0,3,2,1].sort(function(a,b){if(a===0)return1;elsei